package com.mobile.security.webres.utils;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.SecurityConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.util.StringUtils;

/**
 * 
 * @author worldheart
 *
 */
public class RdbmsSecuredUrlDefinition extends MappingSqlQuery{

protected static final Log logger = LogFactory.getLog(RdbmsSecuredUrlDefinition.class);
	
    protected RdbmsSecuredUrlDefinition(DataSource ds) {
        super(ds, "SELECT url, roles FROM webresdb ORDER BY id");
    	logger.debug("进入到RdbmsInvocationDefinition构建器中.........");
        compile();
    }

    protected Object mapRow(ResultSet rs, int rownum)
        throws SQLException {
    	logger.debug("抽取webresdb中的记录.........");
    	RdbmsEntryHolder rsh = new RdbmsEntryHolder();
    	//设置URL
    	rsh.setUrl(rs.getString("url").trim());
    	
        ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
        
        String[] tokens = 
        		StringUtils.commaDelimitedListToStringArray(rs.getString("roles").trim());
        for(int i = 0; i < tokens.length;++i)
        	cad.addConfigAttribute(new SecurityConfig(tokens[i]));
        
        //设置角色集合
        rsh.setCad(cad);
    	
        return rsh;
    }

}